<?php
namespace Kingdee;

use think\facade\Db;

/**
 * 金蝶相关函数方法
 * @author Zl
 */
class KingFunction
{
    /**
     * 登陆方法
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_login($cloudUrl,$post_content,$cookie_jar)
    {
        $loginurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc';
        return self::invoke_post($loginurl,$post_content,$cookie_jar,TRUE);
    }

    /**
     * 保存
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_save($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    /**
     * 下推
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_push($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExcuteOperation.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    /**
     * 审核
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_audit($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    /**
     * 提交
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_submit($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    /**
     * 查看
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_view($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    /**
     * 删除
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_del($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    /**
     * 单据查询
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_BillQuery($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    /**
     * post请求方法
     * @param $url
     * @param $post_content
     * @param $cookie_jar
     * @param $isLogin
     * @return bool|string
     */
    public static function invoke_post($url,$post_content,$cookie_jar,$isLogin)
    {
        $ch = curl_init($url);
        $this_header = array(
            'Content-Type: application/json',
            'Content-Length: '.strlen($post_content)
        );
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过htpps验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);//跳过htpps验证

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if($isLogin){
            // 获取头部信息
            curl_setopt($ch, CURLOPT_HEADER, 1);
            //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        }
        else{
            //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
            curl_setopt($ch, CURLOPT_COOKIE, $cookie_jar);//设置cookie
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 500);//不限时

        $result = curl_exec($ch);
        curl_close($ch);


        return $result;
    }

    /**
     * 构造Web API请求格式
     *
     */
    public static function create_postdata($args)
    {
        $postdata = array(
            'format'=>1,
            'useragent'=>'ApiClient',
            'rid'=>self::create_guid(),
            'parameters'=>$args,
            'timestamp'=>date('Y-m-d'),
            'v'=>'1.0'
        );
        return json_encode($postdata);
    }

    /**
     * 生成guid
     *
     */
    public static function create_guid() {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
            .substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12)
            .chr(125);// "}"
        return $uuid;
    }

    /**
     * 获取金蝶客户编号
     * @param $user_id
     * @return
     */
    public static function get_king_no($user_id){
        return Db::name('customer')->where(['id'=>$user_id])->value('king_no');
    }


    /**
     * 获取金蝶客户编号
     * @param $user_id
     * @return
     */
    public static function get_shop_no($shop_id){
        return Db::name('shop')->where(['shop_id'=>$shop_id])->value('keycode');
    }


    /**
     * 金蝶单位转换
     * $flag 为0转换为最小单位 为1转换为最大单位
     * @param $unit
     * @param int $flag
     * @return float|int
     */
    public static function trans_unit($unit,$flag=0)
    {
        $where['big_unit_code'] = $unit;//销售单位是否在大单位中
        $res = Db::name('shop_goods_unit')->where($where)->find();
        if ($res) {//找到，则是大包装
            $rate = $res['deno'] * $res['mole'];//1乘以转换系数
            return $rate;
        } else {//如果找不到，则是中包装
            return 1;
        }
    }

    /**
     * 反审核
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_un_audit($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }


    /**
     * 禁用|启用
     * @param $cloudUrl
     * @param $post_content
     * @param $cookie_jar
     * @return bool|string
     */
    public static function invoke_operation($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExcuteOperation.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

}
